package com.bnzj.core.persistence.query;

/**
 * 比较类型
 * @author Administrator
 *
 */
public class CompareType {
    public static final CompareType expression = new CompareType(CompareOperator.expression);
    
    public static final CompareType between = new CompareType(CompareOperator.between);
    
    public static final CompareType like = new CompareType(CompareOperator.like);
    
    public static final CompareType startsWith = new CompareType(CompareOperator.startsWith);
    
    public static final CompareType endWith = new CompareType(CompareOperator.endWith);
    
    public static final CompareType equal = new CompareType(CompareOperator.equal);
    
    public static final CompareType in = new CompareType(CompareOperator.in);
    
    public static final CompareType greaterThan = new CompareType(CompareOperator.greaterThan);
    
    public static final CompareType greaterThanOrEqualTo = new CompareType(CompareOperator.greaterThanOrEqualTo);
    
    public static final CompareType lessThan = new CompareType(CompareOperator.lessThan);
    
    public static final CompareType lessThanOrEqualTo = new CompareType(CompareOperator.lessThanOrEqualTo);
    
    public static final CompareType notBetween = new CompareType(CompareOperator.between, true);
    
    public static final CompareType notEqual = new CompareType(CompareOperator.equal, true);
    
    public static final CompareType notIn = new CompareType(CompareOperator.in, true);
    
    public static final CompareType notLike = new CompareType(CompareOperator.like, true);
    
    public static final CompareType notStartsWith = new CompareType(CompareOperator.startsWith, true);
    
    public static final CompareType notEndWith = new CompareType(CompareOperator.endWith, true);

    public String getKeyword() {
        if (negated) {
            if (compareOperator == CompareOperator.equal) {
                return "!" + keyword;
            } else if (compareOperator == CompareOperator.lessThan) {
                return CompareType.greaterThanOrEqualTo.keyword;
            } else if (compareOperator == CompareOperator.lessThanOrEqualTo) {
                return CompareType.greaterThan.keyword;
            } else if (compareOperator == CompareOperator.greaterThan) {
                return CompareType.lessThanOrEqualTo.keyword;
            } else if (compareOperator == CompareOperator.greaterThanOrEqualTo) {
                return CompareType.lessThan.keyword;
            }else {
                return "not " + keyword;
            }
        } else {
            return keyword;
        }
    }
    
    private CompareType(CompareOperator compareOperator) {
        this(compareOperator, false);
    }
	
	private CompareType(CompareOperator compareOperator, boolean negated) {
	    this.compareOperator = compareOperator;
        this.keyword = compareOperator.getKeyword();
	    this.negated = negated;
	}
	
	private final boolean negated;//是否是not条件
    
    private final String keyword;//比较条件关键字
    
    private final CompareOperator compareOperator;
    
    public String isKeyword() {
        return keyword;
    }
    
    public CompareOperator getCompareOperator() {
        return compareOperator;
    }
	
    public boolean isNegated() {
        return negated;
    }
    
    /**
     * 获取not条件
     * @return
     */
    public CompareType getNegatedCompareType() {
        return new CompareType(this.getCompareOperator(), !this.isNegated());
    }
}
